{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ec73ff0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "m\n",
      "hary\n"
     ]
    }
   ],
   "source": [
    "names_str = \"mary,jack,park\"  # 文本序列的操作方法和列表序列有区别\n",
    "names_list = [\"hary\",\"jon\",\"your\"]\n",
    "\n",
    "print(names_str[0]) # 字符串我无法确定名字长度？用切片的复杂操作 [0:4]\n",
    "print(names_list[0]) # 用index索引的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "493f48ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['mary', 'jack', 'jan', 'park', 'zhichao']\n"
     ]
    }
   ],
   "source": [
    "# 增加/新增数据\n",
    "names = [\"mary\",\"jack\",\"jan\",\"park\"]\n",
    "# 2.1  append() 圆括号的内容是对象（比较大的一个范围，python中一切皆是对象）\n",
    "#      append() 新增在末尾\n",
    "names.append(\"zhichao\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c9fb3e6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['mary', 'jack', 'xinyi', 'jan', 'park', 'zhichao']\n"
     ]
    }
   ],
   "source": [
    "# 2.2 insert() 指定位置新增数据\n",
    "# 列表可以出现重复项\n",
    "names.insert(2,\"xinyi\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9ae29aa8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['mary', 'jack', 'jan', 'park', 'zhichao']\n"
     ]
    }
   ],
   "source": [
    "# 3.删除\n",
    "# 3.1 remove() 的操作应该先做查询，判断元素value是否存在\n",
    "names.remove(\"xinyi\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "75f4c712",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "names:lihua\n",
      "['mary', 'jack', 'jan', 'park', 'zhichao', 'lihua']\n",
      "['mary', 'jack', 'jan', 'park', 'zhichao']\n"
     ]
    }
   ],
   "source": [
    "# 查询： seq的一般方法 (通用方法)\n",
    "print(\"lihua\" in names)\n",
    "username = input(\"names:\")\n",
    "if username in names:\n",
    "    # 删除该用户\n",
    "    names.remove(username)\n",
    "else:\n",
    "    names.append(username)\n",
    "print(names)\n",
    "names.remove(\"lihua\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "5d134e24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "['mary', 'jack', 'jan', 'park']\n",
      "['mary', 'jack', 'park']\n"
     ]
    }
   ],
   "source": [
    "# 3.2 pop() 在使用pop()之前，要先了解list的长度？清楚删除的索引位置\n",
    "# 要先了解list的长度？ seq 一般方法 len()\n",
    "names = [\"mary\",\"jack\",\"jan\",\"park\"]\n",
    "print(len(names))\n",
    "print(names)\n",
    "# pop()按index索引删除\n",
    "names.pop(2)\n",
    "print(names)\n",
    "# names.pop(100)\n",
    "# print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "bae5c4ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# 清楚删除的索引位置？\n",
    "names = [\"mary\",\"jack\",\"jan\",\"park\"]\n",
    "print(names.index(\"jan\"))\n",
    "\n",
    "# 案例：删除一些具有特征的索引值\n",
    "## for i in range(0,len(names)-1,2)\n",
    "##     names.pop(i)\n",
    "## print(names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "3e6bcd60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "### 高级用法！！！\n",
    "# 4.删除列表\n",
    "names_wang1 = [\"mary\",\"jack\",\"jan\",\"park\"]\n",
    "names_wang2 = [\"mary\",\"jack\",\"jan\",\"park\"]\n",
    "\n",
    "names.clear()\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "68606c41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['mary', 'jack', 'jan', 'park', 'hary', 'jon', 'your']\n"
     ]
    }
   ],
   "source": [
    "# 5.合并列表 extend 是有顺序的，（）里面的list排后面\n",
    "names_wang1 = [\"mary\",\"jack\",\"jan\",\"park\"]\n",
    "names_wang2 = [\"hary\",\"jon\",\"your\"]\n",
    "\n",
    "names_wang1.extend(names_wang2)\n",
    "print(names_wang1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "47d786f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['mary', 'jack', 'jan', 'park', 'youge']\n",
      "['mary', 'jack', 'jan', 'park', 'youge']\n",
      "nama_wang1: ['mary', 'jack', 'jan', 'park', 'youge']\n",
      "names_wang1复制: ['mary', 'jack', 'jan', 'park', 'youge']\n"
     ]
    }
   ],
   "source": [
    "# 6.复制列表 copy() 赋值不等于真正意义上的复制\n",
    "names_wang1复制 = names_wang1\n",
    "print(names_wang1)\n",
    "print(names_wang1复制)\n",
    "names_wang1 = [\"mary\",\"jack\",\"jan\",\"park\"]\n",
    "names_wang1.append(\"youge\")\n",
    "print(\"nama_wang1:\",names_wang1)\n",
    "print(\"names_wang1复制:\",names_wang1复制)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "5909d0ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['mary', 'jack', 'jan', 'park']\n",
      "['mary', 'jack', 'jan', 'park']\n",
      "nama_wang1: ['mary', 'jack', 'jan', 'park', 'youge']\n",
      "names_wang1复制: ['mary', 'jack', 'jan', 'park']\n"
     ]
    }
   ],
   "source": [
    "# 引入copy\n",
    "names_wang1 = [\"mary\",\"jack\",\"jan\",\"park\"]\n",
    "\n",
    "names_wang1复制 = names_wang1.copy()\n",
    "print(names_wang1)\n",
    "print(names_wang1复制)\n",
    "names_wang1.append(\"youge\")\n",
    "print(\"nama_wang1:\",names_wang1)\n",
    "print(\"names_wang1复制:\",names_wang1复制)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "b400a9a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['mary', 'jack', 'jan', 'park']\n",
      "['mary', 'jack', 'jan', 'park']\n",
      "nama_wang1: ['mary', 'jack', 'jan', 'park', 'youge']\n",
      "names_wang1复制: ['mary', 'jack', 'jan', 'park']\n"
     ]
    }
   ],
   "source": [
    "# 用切片[:] 等价于 copy()\n",
    "names_wang1 = [\"mary\",\"jack\",\"jan\",\"park\"]\n",
    "\n",
    "names_wang1复制 = names_wang1[:]\n",
    "print(names_wang1)\n",
    "print(names_wang1复制)\n",
    "names_wang1.append(\"youge\")\n",
    "print(\"nama_wang1:\",names_wang1)\n",
    "print(\"names_wang1复制:\",names_wang1复制)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "1a999a53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "stu_id = [90,95,99,90,92,90,98]\n",
    "# 7. count() 查看元素出现的次数/统计数量\n",
    "print(stu_id.count(90))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "0b06b136",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[90, 90, 90, 92, 95, 98, 99]\n"
     ]
    }
   ],
   "source": [
    "# 8.排序 sort()\n",
    "stu_id = [90,95,99,90,92,90,98]\n",
    "stu_id.sort()\n",
    "print(stu_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "bdfa1673",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[99, 98, 95, 92, 90, 90, 90]\n"
     ]
    }
   ],
   "source": [
    "# 8.1 reverse=True 从高到低排序\n",
    "stu_id = [90,95,99,90,92,90,98]\n",
    "stu_id.sort(reverse=True)\n",
    "print(stu_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "f41262ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串的排序是a-z的顺序\n",
    "\n",
    "# pop() 按index索引删除 如果不填则删除末位   append()增加末位\n",
    "\n",
    "# 列表元素的合并  'xxxxx'.join"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
